// pages/user/myRest/addRest/addRest.js
var dateTimePicker = require('dateTimePicker.js');
let App = getApp();
// let app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: { 
        date: '2018-10-01', 
        time: '12:00',
        dateTimeArray: null,
        dateTime: null,
        dateTimeArray1: null,
        dateTime1: null,
        startYear: 2000,
        starTime: '',
        starDate: '',
        endTime: '',
        endDate: '',
        plears: '请选择',
        endYear: 2050
        
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var This = this;
        var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
        var obj1 = dateTimePicker.dateTimePicker1(this.data.startYear, this.data.endYear);
        This.data.date = obj.dateTimeArray[0] + obj.dateTimeArray[1] + obj.dateTimeArray[2];
        this.setData({
            dateTimeArray: obj.dateTimeArray,
            dateTime: obj.dateTime,
            dateTime1: obj1.dateTime1,
            dateTimeArray1: obj1.dateTimeArray1,
        });
    },
    changeDateTime(e) {
        this.setData({
            dateTime: e.detail.value
        });
    },
    changeDateTimeColumn(e) {
        var tr = null;
        var gy = null;
        var This = this;
        var arr = this.data.dateTime,
            dateArr = this.data.dateTimeArray;
        arr[e.detail.column] = e.detail.value;
        dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
        This.data.starDate = dateArr[0][arr[0]] + '-' + dateArr[1][arr[1]] + '-' + dateArr[2][arr[2]];
        This.data.starTime = dateArr[3][arr[3]] + ':' + dateArr[4][arr[4]];
        tr = This.data.starDate.split("-");
        gy = This.data.starTime.split(":");
        if (gy[1] == 'undefined') {
            gy[1] = '00';
            This.data.starTime = gy[0] + ":" + gy[1];
        }
        if (gy[0] == '00' || gy[0] == '01' || gy[0] == '02' || gy[0] == '03' || gy[0] == '04' || gy[0] == '05' || gy[0] == '06' || gy[0] =='07') {
            gy[0] = '08';
            This.data.starTime = gy[0] + ":" + gy[1];
        }
        console.log(tr)
        if (tr[1] == '04' || tr[1] == '06' || tr[1] == '08' || tr[1] == '09' || tr[1] == '11') {
            if (tr[2] == 'undefined') {
                tr[2] = 30;
                This.data.starDate = tr[0] + "-" + tr[1] + "-" + tr[2];
            }
        }
        if (tr[1] == '02') {
            if (tr[1] % 400 == 0 || tr[1] % 4 == 0 && year % 100 != 0) {
                if (tr[2] == 'undefined') {
                    tr[2] = 29;
                    This.data.starDate = tr[0] + "-" + tr[1] + "-" + tr[2];
                }
            } else {
                if (tr[2] == 'undefined') {
                    tr[2] = 28;
                    This.data.starDate = tr[0] + "-" + tr[1] + "-" + tr[2];
                }
            }
        }
        this.setData({
            plears: '',
            starTime: This.data.starTime,
            starDate: This.data.starDate
        });
    },
    changeDateTime1(e) {
        this.setData({
            dateTime1: e.detail.value
        });
    },
    changeDateTimeColumn1(e) {

        var This = this;
        var tr = null;
        var gy = null;
        var arr = this.data.dateTime1,
            dateArr = this.data.dateTimeArray1;
        arr[e.detail.column] = e.detail.value;
        dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
        This.data.endDate = dateArr[0][arr[0]] + '-' + dateArr[1][arr[1]] + '-' + dateArr[2][arr[2]];
        This.data.endTime = dateArr[3][arr[3]] + ':' + dateArr[4][arr[4]];
        console.log(This.data.endDate)
        tr = This.data.endDate.split("-");
        gy = This.data.endTime.split(":");

        if (gy[1] == 'undefined') {
            gy[1] = '00';
            This.data.endTime = gy[0] + ":" + gy[1];
        }
        if (tr[1] == '04' || tr[1] == '06' || tr[1] == '08' || tr[1] == '09' || tr[1] == '11') {
            if (tr[2] == 'undefined') {
                tr[2] = 30;
                This.data.endDate = tr[0] + "-" + tr[1] + "-" + tr[2];
            }
        }
        if (tr[1] == '02') {
            if (tr[1] % 400 == 0 || tr[1] % 4 == 0 && year % 100 != 0) {
                if (tr[2] == 'undefined') {
                    tr[2] = 29;
                    This.data.endDate = tr[0] + "-" + tr[1] + "-" + tr[2];
                }
            } else {
                if (tr[2] == 'undefined') {
                    tr[2] = 28;
                    This.data.endDate = tr[0] + "-" + tr[1] + "-" + tr[2];
                }
            }
        }
        this.setData({
            plears: ' ',
            endDate: This.data.endDate,
            endTime: This.data.endTime,
        });
    },
    type_time1: function(e) {
        console.log(e.detail.value[2] + 1)
        var This = this;

        if (e.detail.value[2] + 1 == undefined) {
            console.log("123")
            e.detail.value[2] == '';
        }

    },
    //提交数据
    add_to: function() {
        var This = this;
        console.log(This.data.starDate)
        console.log(This.data.starTime)
        if (This.data.starDate == '' || This.data.starTime==''){
            wx.showToast({
                title: '时间选择不能为空',
                icon:'none',
                duration:2000
            })
        }else {
            App._get('coach/personal/new_Rest_add', {
                start_date: This.data.starDate,
                start_time: This.data.starTime,
                end_date: This.data.endDate,
                end_time: This.data.endTime,
                phone: wx.getStorageSync('phone'),
                openid: wx.getStorageSync('openid'),
                store_id: wx.getStorageSync('store_id'),
            }, function (res) {
                if (res.status == 1) {
                    wx.switchTab({
                        url: '../user/user',
                    })
                    wx.showToast({
                        title: '添加成功',
                        icon: 'none',
                        duration: 2000
                    })
                } else {
                    wx.showToast({
                        title: res.error,
                        icon: 'none',
                        duration: 2000
                    })
                }
            })
        } 
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})